/*
*/
#include "stack.h"

#include <stdlib.h>

int IsStackEmpty(Stack S)
{
    return (NULL == S->next);
}

void MakeStackEmpty(Stack S)
{
    if(NULL == S)
        return;

    while(0 == IsStackEmpty(S))
       PopStack(S); 
}

Stack CreateStack(void)
{
    Stack s;

    s = (Stack)malloc(sizeof(struct stack_node));
    if(NULL == s)
        return NULL;

    s->next = NULL;     // 无须用 MakeStackEmpty,因为此时，不存在清空原栈残余的必要
    return s;
}

void DisposeStack(Stack S)
{
    if(NULL == S)
        return;

    MakeStackEmpty(S);
    free(S);
}

void PushStack(ElementStack X,Stack S)
{
    PStackToNode p;

    p = (PStackToNode)malloc(sizeof(struct stack_node));
    if(NULL == p)
    {
        return;
    }

    p->e = X;
    p->next = S->next;

    S->next = p;
}

ElementStack TopStack(Stack S)
{
    if(NULL == S->next)
        return -1;

    return S->next->e;
}

void PopStack(Stack S)
{
    PStackToNode p;

    if(1 == IsStackEmpty(S))
        return;

    p = S->next;
    S->next = p->next;

    free(p); 
}

// end of file ----------------------------------------------------------------
